home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP06.ZIP / CHAP06 / POLYLINE / IPOLYLIN.CPP < prev    next >
C/C++ Source or Header  |  1993-06-17  |  13KB  |  628 lines

  1. /*
  2.  * IPOLYLIN.CPP
  3.  * Polyline Component Object Chapter 6
  4.  *
  5.  * Implementation of the IPolyline interface that we expose on the
  6.  * CPolyline object.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #include "polyline.h"
  19.  
  20.  
  21. /*
  22.  * CImpIPolyline:CImpIPolyline
  23.  * CImpIPolyline::~CImpIPolyline
  24.  *
  25.  * Constructor Parameters:
  26.  *  pObj            LPVOID pointing to the object we live in.
  27.  *  punkOuter       LPUNKNOWN of the controlling unknown.
  28.  */
  29.  
  30. CImpIPolyline::CImpIPolyline(LPVOID pObj, LPUNKNOWN punkOuter)
  31.     {
  32.     m_cRef=0;
  33.     m_pObj=pObj;
  34.     m_punkOuter=punkOuter;
  35.     return;
  36.     }
  37.  
  38.  
  39. CImpIPolyline::~CImpIPolyline(void)
  40.     {
  41.     return;
  42.     }
  43.  
  44.  
  45.  
  46.  
  47. /*
  48.  * CImpIPolyline::QueryInterface
  49.  * CImpIPolyline::AddRef
  50.  * CImpIPolyline::Release
  51.  *
  52.  * Purpose:
  53.  *  Standard set of IUnknown members for this interface
  54.  */
  55.  
  56. STDMETHODIMP CImpIPolyline::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  57.     {
  58.     return m_punkOuter->QueryInterface(riid, ppv);
  59.     }
  60.  
  61. STDMETHODIMP_(ULONG) CImpIPolyline::AddRef(void)
  62.     {
  63.     ++m_cRef;
  64.     return m_punkOuter->AddRef();
  65.     }
  66.  
  67. STDMETHODIMP_(ULONG) CImpIPolyline::Release(void)
  68.     {
  69.     --m_cRef;
  70.     return m_punkOuter->Release();
  71.     }
  72.  
  73.  
  74.  
  75.  
  76.  
  77. /*
  78.  * CImpIPolyline::Init
  79.  *
  80.  * Purpose:
  81.  *  Instantiates a polyline window within a given parent.  The
  82.  *  parent may be a main application window, could be an MDI child
  83.  *  window. We really do not care.
  84.  *
  85.  * Parameters:
  86.  *  hWndParent      HWND of the parent of this window
  87.  *  pRect           LPRECT that this window should occupy
  88.  *  dwStyle         DWORD containing the window's style flags
  89.  *  uID             UINT ID to associate with this window
  90.  *
  91.  * Return Value:
  92.  *  HRESULT         NOERROR if successful, otherwise E_OUTOFMEMORY
  93.  */
  94.  
  95. STDMETHODIMP CImpIPolyline::Init(HWND hWndParent, LPRECT pRect
  96.     , DWORD dwStyle, UINT uID)
  97.     {
  98.     LPCPolyline     pObj=(LPCPolyline)m_pObj;
  99.     SCODE           sc;
  100.  
  101.     pObj->m_hWnd=CreateWindowEx(WS_EX_NOPARENTNOTIFY, SZCLASSPOLYLINE
  102.         , SZCLASSPOLYLINE, dwStyle, pRect->left, pRect->top
  103.         , pRect->right-pRect->left, pRect->bottom-pRect->top
  104.         , hWndParent, (HMENU)uID, pObj->m_hInst, (LPVOID)pObj);
  105.  
  106.  
  107.     sc=(NULL!=pObj->m_hWnd) ? S_OK : E_OUTOFMEMORY;
  108.     return ResultFromScode(sc);
  109.     }
  110.  
  111.  
  112.  
  113.  
  114. //CHAPTER6MOD
  115. /*
  116.  * ::Data* and ::Render* memebers moved into IDataObject.
  117.  */
  118. //CHAPTER6MOD
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. /*
  127.  * CImpIPolyline::New
  128.  *
  129.  * Purpose:
  130.  *  Cleans out and reinitializes the data to defaults.
  131.  *
  132.  * Parameters:
  133.  *  None
  134.  *
  135.  * Return Value:
  136.  *  HRESULT         NOERROR always
  137.  */
  138.  
  139. STDMETHODIMP CImpIPolyline::New(void)
  140.     {
  141.     LPCPolyline     pObj=(LPCPolyline)m_pObj;
  142.     LPPOLYLINEDATA  ppl=&pObj->m_pl;
  143.     UINT            i;
  144.  
  145.     ppl->wVerMaj=VERSIONMAJOR;
  146.     ppl->wVerMin=VERSIONMINOR;
  147.  
  148.     //Our rectangle is the size of our window's client area.
  149.     GetClientRect(pObj->m_hWnd, &ppl->rc);
  150.  
  151.     //Clean out the POLYLINEDATA structure and repaint the window.
  152.     for (i=0; i< CPOLYLINEPOINTS; i++)
  153.         {
  154.         ppl->rgpt[i].x=0;
  155.         ppl->rgpt[i].y=0;
  156.         }
  157.  
  158.     ppl->cPoints      =0;
  159.     ppl->rgbBackground=GetSysColor(COLOR_WINDOW);
  160.     ppl->rgbLine      =GetSysColor(COLOR_WINDOWTEXT);
  161.     ppl->iLineStyle   =PS_SOLID;
  162.  
  163.     InvalidateRect(pObj->m_hWnd, NULL, TRUE);
  164.     UpdateWindow(pObj->m_hWnd);
  165.  
  166.     pObj->m_fDirty=TRUE;
  167.  
  168.     //CHAPTER6MOD
  169.     //Inform the advise sink of this data change.
  170.     if (NULL!=pObj->m_pIDataAdviseHolder)
  171.         {
  172.         pObj->m_pIDataAdviseHolder->SendOnDataChange(pObj->m_pIDataObject
  173.             , DVASPECT_CONTENT, ADVF_NODATA);
  174.         }
  175.     //End CHAPTER6MOD
  176.  
  177.     return NOERROR;
  178.     }
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185. /*
  186.  * CImpIPolyline::Undo
  187.  *
  188.  * Purpose:
  189.  *  Reverses previous actions in a Polyline.
  190.  *
  191.  * Parameters:
  192.  *  None
  193.  *
  194.  * Return Value:
  195.  *  HRESULT         S_OK if we can Undo more, S_FALSE otherwise.
  196.  */
  197.  
  198. STDMETHODIMP CImpIPolyline::Undo(void)
  199.     {
  200.     LPCPolyline     pObj=(LPCPolyline)m_pObj;
  201.     SCODE           sc;
  202.  
  203.     //Decrement the number of active points and repaint.
  204.     if (pObj->m_pl.cPoints > 0)
  205.         {
  206.         pObj->m_pl.cPoints--;
  207.         InvalidateRect(pObj->m_hWnd, NULL, TRUE);
  208.         UpdateWindow(pObj->m_hWnd);
  209.         }
  210.  
  211.     if (NULL!=pObj->m_pAdv)
  212.         {
  213.         pObj->m_fDirty=TRUE;
  214.         pObj->m_pAdv->OnPointChange();
  215.         }
  216.  
  217.     //Return if we can undo any more.
  218.     sc=(0!=pObj->m_pl.cPoints) ? S_OK : S_FALSE;
  219.     return ResultFromScode(sc);
  220.     }
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227. /*
  228.  * CImpIPolyline::Window
  229.  *
  230.  * Purpose:
  231.  *  Returns the window handle associated with this polyline.
  232.  *
  233.  * Parameters:
  234.  *  phWnd           HWND FAR * in which to return the window handle.
  235.  *
  236.  * Return Value:
  237.  *  HRESULT         NOERROR always.
  238.  */
  239.  
  240. STDMETHODIMP CImpIPolyline::Window(HWND FAR *phWnd)
  241.     {
  242.     LPCPolyline     pObj=(LPCPolyline)m_pObj;
  243.  
  244.     *phWnd=pObj->m_hWnd;
  245.     return NOERROR;
  246.     }
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253. /*
  254.  * CImpIPolyline::SetAdvise
  255.  *
  256.  * Purpose:
  257.  *  Provides this object with an IPolylineAdviseSink that's interested
  258.  *  in our notifications.  We AddRef and store this pointer, Releasing
  259.  *  the old one.
  260.  *
  261.  * Parameters:
  262.  *  pAdv            LPPOLYLINEADVISESINK to notify.
  263.  *
  264.  * Return Value:
  265.  *  HRESULT         NOERROR always.
  266.  */
  267.  
  268. STDMETHODIMP CImpIPolyline::SetAdvise(LPPOLYLINEADVISESINK pAdv)
  269.     {
  270.     LPCPolyline     pObj=(LPCPolyline)m_pObj;
  271.  
  272.     if (NULL!=pObj->m_pAdv)
  273.         pObj->m_pAdv->Release();
  274.  
  275.     pObj->m_pAdv=pAdv;
  276.  
  277.     if (NULL!=pObj->m_pAdv)
  278.         pObj->m_pAdv->AddRef();
  279.  
  280.     return NOERROR;
  281.     }
  282.  
  283.  
  284.  
  285.  
  286. /*
  287.  * CImpIPolyline::GetAdvise
  288.  *
  289.  * Purpose:
  290.  *  Returns the IPolylineAdviseSink that was last passed to SetAdvise.
  291.  *
  292.  * Parameters:
  293.  *  ppAdv           LPPOLYLINEADVISESINK FAR * in which to return the
  294.  *                  pointer.
  295.  *
  296.  * Return Value:
  297.  *  HRESULT         NOERROR always.
  298.  */
  299.  
  300. STDMETHODIMP CImpIPolyline::GetAdvise(LPPOLYLINEADVISESINK FAR *ppAdv)
  301.     {
  302.     LPCPolyline     pObj=(LPCPolyline)m_pObj;
  303.  
  304.     *ppAdv=pObj->m_pAdv;
  305.     return NOERROR;
  306.     }
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313. /*
  314.  * CImpIPolyline::RectGet
  315.  *
  316.  * Purpose:
  317.  *  Returns the rectangle of the Polyline in parent coordinates.
  318.  *
  319.  * Parameters:
  320.  *  pRect           LPRECT in which to return the rectangle.
  321.  *
  322.  * Return Value:
  323.  *  HRESULT         NOERROR always
  324.  */
  325.  
  326. STDMETHODIMP CImpIPolyline::RectGet(LPRECT pRect)
  327.     {
  328.     LPCPolyline     pObj=(LPCPolyline)m_pObj;
  329.     RECT            rc;
  330.     POINT           pt;
  331.  
  332.     //Retrieve the size of our rectangle in parent coordinates.
  333.     GetWindowRect(pObj->m_hWnd, &rc);
  334.     pt.x=rc.left;
  335.     pt.y=rc.top;
  336.     ScreenToClient(GetParent(pObj->m_hWnd), &pt);
  337.  
  338.     SetRect(pRect, pt.x, pt.y, pt.x+(rc.right-rc.left)
  339.         , pt.y+(rc.bottom-rc.top));
  340.  
  341.     return NOERROR;
  342.     }
  343.  
  344.  
  345.  
  346.  
  347.  
  348. /*
  349.  * CImpIPolyline::SizeGet
  350.  *
  351.  * Purpose:
  352.  *  Retrieves the size of the Polyline in parent coordinates.
  353.  *
  354.  * Parameters:
  355.  *  pRect           LPRECT in which to return the size.  The right and
  356.  *                  bottom fields will contain the dimensions.
  357.  *
  358.  * Return Value:
  359.  *  HRESULT         NOERROR always
  360.  */
  361.  
  362. STDMETHODIMP CImpIPolyline::SizeGet(LPRECT pRect)
  363.     {
  364.     RectGet(pRect);
  365.     return NOERROR;
  366.     }
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373. /*
  374.  * CImpIPolyline::RectSet
  375.  *
  376.  * Purpose:
  377.  *  Sets a new rectangle for the Polyline which sizes to fit.
  378.  *
  379.  * Parameters:
  380.  *  pRect           LPRECT containing the new rectangle.
  381.  *  fNotify         BOOL indicating if we're to notify anyone of the change.
  382.  *
  383.  * Return Value:
  384.  *  HRESULT         NOERROR always
  385.  */
  386.  
  387. STDMETHODIMP CImpIPolyline::RectSet(LPRECT pRect, BOOL fNotify)
  388.     {
  389.     LPCPolyline     pObj=(LPCPolyline)m_pObj;
  390.     UINT            cx, cy;
  391.  
  392.     //Scale the points from our current size to the new size
  393.     cx=pRect->righ